package com.lagou.edu.util;

/**
 * @ClassName: HttpProtocolUtil
 * @Auther: Jerry
 * @Date: 2020/7/30 22:48
 * @Desctiption: TODO
 * @Version: 1.0
 */
public class HttpProtocolUtil {

    /**
     * 构建请求为200的请求头信息
     * @param contentLen 内容Len
     * @return {@link String}
     */
    public static String getHeader200(long contentLen) {
        StringBuilder sb = new StringBuilder();
        sb.append("HTTP/1.1 200 OK\n");
        sb.append("Content-Type: text/html;charset=utf-8\n");
        sb.append("Content-Length: ");
        sb.append(contentLen);
        sb.append("\n");
        sb.append("\r\n");
        return sb.toString();
    }

    /**
     * 构建请求为404的请求头信息
     * @return {@link String}
     */
    public static String getHeader404() {
        StringBuilder sb = new StringBuilder();
        String content = "<h1>404 NOT FOUND</h1>";
        sb.append("HTTP/1.1 200 OK\n");
        sb.append("Content-Type: text/html;charset=utf-8\n");
        sb.append("Content-Length: ");
        sb.append(content.getBytes().length);
        sb.append("\n");
        sb.append("\r\n");
        sb.append(content);
        return sb.toString();
    }
}
